#include <imageButton.h>
#include <uiResourceManager.h>

namespace lgb_gtk
{

  gboolean OnImageButtonPress(GooCanvasItem *item,
			      GooCanvasItem *target_item,
			      GdkEventButton *event,
			      gpointer user_data)
  {
    ImageButton *button = (ImageButton *)user_data;
    button->SetCurStat(BUTTON_STAT_MOUSE_DOWN);
    return true;
  }

  gboolean OnImageButtonMouseOver(GooCanvasItem *item,
				  GooCanvasItem *target_item,
				  GdkEventButton *event,
				  gpointer user_data)
  {
    ImageButton *button = (ImageButton *)user_data;
    button->SetCurStat(BUTTON_STAT_MOUSE_OVER);
    return true;
  }

  gboolean OnImageButtonMouseOut(GooCanvasItem *item,
				 GooCanvasItem *target_item,
				 GdkEventButton *event,
				 gpointer user_data)
  {
    ImageButton *button = (ImageButton *)user_data;
    button->SetCurStat(BUTTON_STAT_NORMAL);
    return true;
  }

  ImageButton::ImageButton()
  {
    bgCanvas = NULL;
    igCanvas = NULL;
    cgCanvas = NULL;
    bgPath = "";
    igPath = "";
    cgPath = "";
    curStat = BUTTON_STAT_NORMAL;
  }

  ImageButton::~ImageButton()
  {

  }

  int ImageButton::LoadAllCanvas()
  {
    if(NULL == bgCanvas && "" != bgPath)
      {
	bgCanvas = goo_canvas_image_new( canvas ,
					 UiResourceManager::GetInstance()->CreatePixbuf(bgPath),
					 0,0,NULL);
	g_object_set(bgCanvas,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
      }
    if(NULL == igCanvas && "" != igPath)
      {
	igCanvas = goo_canvas_image_new( canvas ,
					 UiResourceManager::GetInstance()->CreatePixbuf(igPath),
					 0,0,NULL);
	g_object_set(igCanvas,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
      }
    if(NULL == cgCanvas && "" != cgPath)
      {
	cgCanvas = goo_canvas_image_new( canvas ,
					 UiResourceManager::GetInstance()->CreatePixbuf(cgPath),
					 0,0,NULL);
	g_object_set(cgCanvas,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
      }
    return 0;
  }

  int ImageButton::SetLabel(string newLabel) 
  {
    return 0;
  }

  int ImageButton::SetImages(string bg, string ig , string cg) 
  {
    bgPath = bg;
    igPath = ig;
    cgPath = cg;
    if(hasInit)
      {
	LoadAllCanvas();
      }
    return 0;
  }

  int ImageButton::Init()
  {
    if(hasInit)
      return 1;
    LoadAllCanvas();
    g_signal_connect(G_OBJECT (canvas) , "button-press-event",
		     G_CALLBACK (OnImageButtonPress) , this);
    g_signal_connect(G_OBJECT (canvas) , "motion-notify-event",
		     G_CALLBACK (OnImageButtonMouseOver) , this);
    g_signal_connect(G_OBJECT (canvas) , "leave-notify-event",
		     G_CALLBACK (OnImageButtonMouseOut) , this);
 
    hasInit = true;
    return 0;
  }

  int ImageButton::SetCurStat(ButtonStat stat)
  {
    if(stat == curStat)
      return 1;
    curStat = stat;
    GooCanvasItem *show(NULL),*hide1(NULL),*hide2(NULL);
    switch(stat)
      {
      case BUTTON_STAT_NORMAL:
	MouseOutBaseFunction();
	if(NULL != bgCanvas)
	  {
	    show = bgCanvas;
	    hide1 = igCanvas;
	    hide2 = cgCanvas;
	  }
	break;
      case BUTTON_STAT_MOUSE_OVER:
	MouseOverBaseFunction();
	if(NULL != igCanvas)
	  {
	    show = igCanvas;
	    hide1 = bgCanvas;
	    hide2 = cgCanvas;
	  }
	break;
      case BUTTON_STAT_MOUSE_DOWN:
	MouseDownBaseFunction();
	if(NULL != bgCanvas)
          {
            show = cgCanvas;
            hide1 = igCanvas;
            hide2 = bgCanvas;
          }
	break;
      default:
	break;
      };
    if(NULL != show)
      {
	g_object_set(show,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
	if(NULL != hide1)
	  g_object_set(hide1,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
	if(NULL != hide2)
	  g_object_set(hide2,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
      }
    return 0;
  }

}
